package org.gnu.williamjoy.marycircles.activity;

import org.gnu.williamjoy.marycircles.R;
import org.gnu.williamjoy.marycircles.control.GameState;
import org.gnu.williamjoy.marycircles.view.CirclesDrawView;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class MaryCirclesActivity extends Activity {

    CirclesDrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        GameState game = new GameState();
        game.setUpdateInterval(this.getResources().getInteger(
                R.integer.update_interval));
        drawView = new CirclesDrawView(this);
        drawView.setGameState(game);
        drawView.init();
        game.setView(drawView);
        game.initSounds();
        setContentView(drawView);
        drawView.requestFocus();
    }

    @Override
    protected void onPause() {
        super.onPause();
        drawView.pause();
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (this.getCurrentFocus() == drawView) {
            menu.getItem(0).setVisible(true);
            menu.getItem(1).setVisible(false);
        } else {
            menu.getItem(0).setVisible(false);
            menu.getItem(1).setVisible(true);
        }
        return true;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.option_menu_preference:
                setContentView(R.layout.main);
                drawView.pause();
                break;
            case R.id.option_menu_play:
                setContentView(drawView);
                drawView.requestFocus();
                drawView.onContinue();
                break;
            case R.id.option_menu_help:
                Intent i = new Intent(Intent.ACTION_VIEW,
                        Uri.parse("http://code.google.com/p/mary-circles/"));
                startActivity(i);
                break;
            case R.id.option_menu_about:
                showAbout();
                break;
            case R.id.option_menu_exit:
                this.finish();
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

    private void showAbout() {
        AlertDialog.Builder about = new AlertDialog.Builder(this);
        about.setTitle("About");
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View aboutView = (View) inflater.inflate(R.layout.about, null);
        about.setView(aboutView);
        about.setIcon(R.drawable.ic_circles);
        about.setPositiveButton("Ok", null);
        about.show();
    }
}